package com.pkx.cloud.fse7.dms.area.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pkx.cloud.fse7.dms.area.entity.DmsArea;
import com.pkx.cloud.fse7.dms.area.service.DmsAreaService;
import com.pkx.cloud.whzx.comn.ORpose;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制器
 *
 * @author chenwg
 * @since 2020-11-13
 */
@RestController
@RequestMapping("/dms/area")
@Api(value = "区域", tags = "区域")
@Slf4j
@ORpose
public class DmsAreaController {

    @Autowired
    private DmsAreaService dmsAreaService;

    @ApiOperation("分页列表")
    @GetMapping("/page")
    public Page<DmsArea> page(Page page, DmsArea dmsArea) {
        return dmsAreaService.pageList(page, dmsArea);
    }

    @ApiOperation("列表")
    @GetMapping("/list")
    public List<DmsArea> list(DmsArea dmsArea) {
        return dmsAreaService.listByEntity(dmsArea);
    }

    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public DmsArea selectById(@PathVariable("id") Long id) {
        return dmsAreaService.selectById(id);
    }

    @ApiOperation("新增")
    @PostMapping("/")
    public Boolean add(@RequestBody DmsArea dmsArea) {
        return dmsAreaService.saveDmsArea(dmsArea);
    }

    @ApiOperation("修改")
    @PutMapping("/")
    public Boolean modify(@RequestBody DmsArea dmsArea) {
        return dmsAreaService.modify(dmsArea);
    }

    @ApiOperation("删除(单个条目)")
    @DeleteMapping(value = "/{id}")
    public Boolean deleteById(@PathVariable("id") Long id) {
        return dmsAreaService.deleteById(id);
    }

    /**
     * 查询区域
     *
     * @return
     */
    @GetMapping("/treeList")
    @ApiOperation("门禁区域")
    public List<Tree<String>> treeList() {
        List<DmsArea> dmsAreas = dmsAreaService.listByEntity(null);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setDeep(3);
        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(dmsAreas, "-1", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getRid());
                    tree.setParentId(treeNode.getParentId());
                    tree.setName(treeNode.getName());
                });
        return treeNodes;
    }
}
